package com.fieldchain.ex.fa100.ui.c2c.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.C2CListBean;
import com.fieldchain.ex.fa100.ui.c2c.adapter.C2CHistoryAdapter;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequestTwo;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/19
 */
public class C2CHistoryActivity extends MyBaseActivity {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private RadioButton rb0;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private SmartRefreshLayout refresh;
    private RecyclerView rv;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;

    private int index = -1;
    private int page = 1;
    private C2CHistoryAdapter adapter;
    private List<C2CListBean> mList = new ArrayList<>();

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        rb0 = findViewById(R.id.rb0);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rb3 = findViewById(R.id.rb3);
        rb4 = findViewById(R.id.rb4);
        refresh = findViewById(R.id.refresh);
        rv = findViewById(R.id.rv);
        reaEmpty = findViewById(R.id.rea_empty);
        tvEmpty = findViewById(R.id.tv_empty);
    }

    @Override
    public void setValue() {
        reaBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvCenter.setText("兑换记录");
        adapter = new C2CHistoryAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);

        setListener(rb0, -1);
        setListener(rb1, 0);
        setListener(rb2, 1);
        setListener(rb3, 3);
        setListener(rb4, 6);
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                refreshLayout.finishLoadMore(1000);
                getData();
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mList.clear();
                refresh.finishRefresh(1000);
                getData();
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.acticvity_c2chistory;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    public void getData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("status", index + "");
        HttpRequest.getInstance().getRequest(map, Constant.c2cHistory, new ListRequestTwo<C2CListBean>() {
            @Override
            public void onSucess(List<C2CListBean> list, int code, String msg) {

                if (list != null && list.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    if (page == 1) {
                        reaEmpty.setVisibility(View.VISIBLE);
                    }
                }

                if (code == 200 && list != null) {
                    List<C2CListBean> newC2CListBeans = new ArrayList<>(list);
                    newC2CListBeans.removeAll(mList);
                    mList.addAll(newC2CListBeans);
                    adapter.setData(mList);
                }

                if (list == null || list.size() == 0) {
                    if (page > 1) {
                        page--;
                    }
                }
            }

            @Override
            public void onFaild(Exception e) {
                if (page > 1) {
                    page--;
                }

                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    public void setListener(RadioButton rb, final int tag) {
        rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    page = 1;
                    index = tag;
                    mList.clear();
                    getData();
                }
            }
        });
    }

    @Override
    public void loginShow() {
        if (ShareManager.getInstance().getUser() == null) {
            MyToast.show("未登录");
        } else {
            MyToast.show("登录过期");
        }
    }

}
